package cn.jianyongjie.org.controller;

import cn.jianyongjie.basic.utils.AjaxResult;
import cn.jianyongjie.basic.utils.QueryResult;
import cn.jianyongjie.org.domain.Employee;
import cn.jianyongjie.org.domain.SystemDetail;
import cn.jianyongjie.org.domain.SystemDict;
import cn.jianyongjie.org.query.SystemQuery;
import cn.jianyongjie.org.service.ISystemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
* @Description: TODO
* @Author: Jian Yong Jie
* @Version: 1.0
* @Date:  2022/4/22 9:50
*/
@RestController
@RequestMapping("/system")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class SystemController {

    @Autowired
    private ISystemService iSystemService;




    /*
    * @Title: SystemDictController
    * @Description: 查询所有部门数据
    * @Author: Jian Yongjie
    * @Version: 1.0
    * @Date:  2022/4/25 12:55
    * @Parameters: * @param null
    * @Return
    */
    @GetMapping
    public List<SystemDict> findDept(){
        return iSystemService.loadAll();
    }

    /*
     * @Title: DepartmentController
     * @Description: 根据部门id查询员工数据
     * @Author: JianYongJie
     * @Version: 1.0
     * @Date:  2022/4/27 18:45
     * @Parameters: * @param null
     * @Return
     */
    @GetMapping("/findSystemById/{id}")
    public List<SystemDetail> findSystemById(@PathVariable("id") Long id){
        return iSystemService.findSystemById(id);
    }

    /*
    * @Title: SystemDictController
    * @Description: 根据id查询数据
    * @Author: Jian Yongjie
    * @Version: 1.0
    * @Date:  2022/4/22 9:55
    * @Parameters: * @param id
    * @Return cn.jianyongjie.org.domain.SystemDict
    */
    // 使用restful风格，设置请求
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public SystemDict findSystemDictById(@ApiParam(name="ID属性",required = true) @PathVariable("id") long id){

        return iSystemService.loadById(id);
    }

    /*
    * @Title: SystemDictController
    * @Description: 分页查询
    * @Author: Jian Yongjie
    * @Version: 1.0
    * @Date:  2022/4/22 10:01
    * @Parameters: * @param
    * @Return cn.jianyongjie.basic.utils.QueryResult
    */
    @PostMapping
    public QueryResult<SystemDict> finddepartments(@RequestBody SystemQuery systemQuery){
        return iSystemService.queryData(systemQuery);
    }

    /*
    * @Title: SystemDictController
    * @Description: 根据id删除数据
    * @Author: Jian Yongjie
    * @Version: 1.0
    * @Date:  2022/4/22 12:30
    * @Parameters: * @param
    * @Return void
    */
    @DeleteMapping("/{id}")
    public AjaxResult delSystemDict(@PathVariable("id") Long id){
        try {
            // 根据id删除数据
            iSystemService.remove(id);
            // 返回Ajax
            return new AjaxResult();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    @PutMapping
    public AjaxResult addOrUpd(@RequestBody SystemDict department){
        try {
            // 根据id进行新增或者修改
            if (department.getId()==null){
                // 进行新增
                iSystemService.save(department);
            }else {
                // 进行修改
                iSystemService.update(department);
            }
            return new AjaxResult();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    /*
    * @Title: SystemDictController
    * @Description: 批量删除
    * @Author: Jian Yongjie
    * @Version: 1.0
    * @Date:  2022/4/23 12:32
    * @Parameters: * @param ids
    * @Return cn.jianyongjie.basic.utils.AjaxResult
    */
    @PatchMapping
    public  AjaxResult delSystemDicts(@RequestBody List<Long> ids){
        try {
            iSystemService.batchRemove(ids);
            return new AjaxResult();
        }catch (Exception e){
            return AjaxResult.error();
        }
    }

}
